package com.yoyo.system.service.impl;

import com.yoyo.common.utils.DateUtils;
import com.yoyo.common.utils.StringUtils;
import com.yoyo.system.domain.SysUserOnline;
import com.yoyo.system.mapper.SysUserOnlineMapper;
import com.yoyo.system.service.ISysUserOnlineService;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zyl
 * @Date 2021/7/3 -15:45
 */
@Service
public class SysUserOnlineServiceImpl implements ISysUserOnlineService {
    @Autowired
    private SysUserOnlineMapper sysUserOnlineMapper;
    @Autowired
    private EhCacheManager ehCacheManager;

    @Override
    public void deleteOnlineById(String sessionId) {
        SysUserOnline sysUserOnline = selectOnlineById(sessionId);
        if (StringUtils.isNotNull(sysUserOnline)) {
            sysUserOnlineMapper.deleteOnlineById(sessionId);
        }
    }

    @Override
    public List<SysUserOnline> selectOnlineByExpired(Date expireDate) {
        String lastAccessTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, expireDate);

        return sysUserOnlineMapper.selectOnlineByExpired(lastAccessTime);
    }

    @Override
    public void saveOnline(SysUserOnline online) {
        sysUserOnlineMapper.saveOnline(online);
    }

    @Override
    public SysUserOnline selectOnlineById(String sessionId) {
        return sysUserOnlineMapper.selectOnlineById(sessionId);
    }
}
